package com.gitee.qdbp.general.common.biz.oplog.service;

import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.jdbc.ordering.OrderPaging;
import com.gitee.qdbp.able.jdbc.paging.PageList;
import com.gitee.qdbp.able.result.ResultCode;
import com.gitee.qdbp.base.system.model.IAccount;
import com.gitee.qdbp.general.common.api.oplog.model.OperateRecordBean;
import com.gitee.qdbp.general.common.api.oplog.model.OperateRecordOptions;
import com.gitee.qdbp.general.common.api.oplog.model.OperateRecordUpdate;
import com.gitee.qdbp.general.common.api.oplog.model.OperateRecordWhere;
import com.gitee.qdbp.general.common.api.oplog.service.IOperateRecordExecutor;
import com.gitee.qdbp.general.common.biz.oplog.basic.OperateRecordBasic;
import com.gitee.qdbp.general.common.enums.LogProcessState;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 操作记录业务处理类
 *
 * @author zhh
 * @version 170522
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public class OperateRecordExecutor implements IOperateRecordExecutor {

    /** 日志对象 **/
    private static final Logger log = LoggerFactory.getLogger(OperateRecordExecutor.class);

    /** 操作记录DAO **/
    @Autowired
    private OperateRecordBasic operateRecordBasic;

    @Override
    public void create(OperateRecordBean model) throws ServiceException {
        String msg = "Failed to create OperateRecord. ";

        if (VerifyTools.isBlank(model)) {
            log.error(msg + "params is null: model");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 向sys_operate_record表插入记录
        operateRecordBasic.create(model);
    }

    @Override
    public void mark(List<String> ids, LogProcessState state, String desc, IAccount me) throws ServiceException {
        String msg = "Failed to mark OperateRecord. ";

        if (VerifyTools.isBlank(ids)) {
            log.error(msg + "params is null: ids");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 更新状态
        if (VerifyTools.isBlank(desc)) {
            OperateRecordUpdate ud = new OperateRecordUpdate();
            ud.setProcessState(state);
            ud.getWhere(true).setIds(ids);
            operateRecordBasic.update(ud, false);
        } else {
            OperateRecordWhere where = new OperateRecordWhere();
            where.setIds(ids);
            PageList<OperateRecordBean> beans = operateRecordBasic.list(where, OrderPaging.NONE);
            if (beans == null) {
                return;
            }

            for (OperateRecordBean bean : beans) {
                OperateRecordOptions options = bean.getOptions();
                options.addRemark(me.getDisplayName(), desc);

                OperateRecordUpdate ud = new OperateRecordUpdate();
                ud.setProcessState(state);
                ud.setOptions(options);
                ud.getWhere(true).setId(bean.getId());
                operateRecordBasic.update(ud, false);
            }
        }
    }

    @Override
    public void delete(String id) throws ServiceException {
        String msg = "Failed to delete OperateRecord. ";

        if (VerifyTools.isBlank(id)) {
            log.error(msg + "params is null: id");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 删除sys_operate_record表的记录
        operateRecordBasic.deleteByIds(Arrays.asList(id), false);
    }

    @Override
    public void delete(List<String> ids) throws ServiceException {
        String msg = "Failed to delete OperateRecord. ";

        if (VerifyTools.isBlank(ids)) {
            log.error(msg + "params is null: ids");
            throw new ServiceException(ResultCode.PARAMETER_IS_REQUIRED);
        }

        // 删除sys_operate_record表的记录
        operateRecordBasic.deleteByIds(ids, false);
    }

}
